<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="布料编码" prop="fabricCode">
        <el-input
          v-model="queryParams.fabricCode"
          placeholder="请输入布料编码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="布料名称" prop="fabricName">
        <el-input
          v-model="queryParams.fabricName"
          placeholder="请输入布料名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="布料厂商名称" prop="fabricManufacturer" label-width="100px">
        <el-input
          v-model="queryParams.fabricManufacturer"
          placeholder="请输入布料厂商名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:fabric:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:fabric:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:fabric:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:fabric:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fabricList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" :index="1"/>
      <el-table-column label="布料编码" align="center" prop="fabricCode" sortable/>
      <el-table-column label="布料名称" align="center" prop="fabricName" sortable/>
      <el-table-column label="布料图片" align="center" prop="fabricImagePath" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.fabricImagePath" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="当前库存(㎡)" align="center" prop="nowInventory" sortable/>
      <el-table-column label="库存告警数值(㎡)" align="center" prop="alertLessThanCount" sortable/>
      <el-table-column label="布料厂商名称" align="center" prop="fabricManufacturer"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:fabric:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:fabric:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改布料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="布料编码" prop="fabricCode">
              <el-input v-model="form.fabricCode" placeholder="请输入布料编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="布料名称" prop="fabricName">
              <el-input v-model="form.fabricName" placeholder="请输入布料名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="图片路径" prop="fabricImagePath">
              <image-upload v-model="form.fabricImagePath"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="当前库存" prop="nowInventory">
              <el-input v-model="form.nowInventory" placeholder="请输入当前库存(单位：㎡)"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库存告警数值" prop="alertLessThanCount">
              <el-input v-model="form.alertLessThanCount" placeholder="请输入库存告警数值(单位：㎡)"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="布料厂商名称" prop="fabricManufacturer">
              <el-input v-model="form.fabricManufacturer" placeholder="请输入布料厂商名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listFabric, getFabric, delFabric, addFabric, updateFabric} from "@/api/product/fabric";

export default {
  name: "Fabric",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 布料表格数据
      fabricList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fabricCode: null,
        fabricName: null,
        fabricManufacturer: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fabricCode: [
          {required: true, message: "布料编码不能为空", trigger: "blur"}
        ],
        fabricImagePath: [
          {required: true, message: "布料图片路径不能为空", trigger: "blur"}
        ],
        nowInventory: [
          {required: true, message: "当前库存(单位：㎡)不能为空", trigger: "blur"}
        ],
        fabricManufacturer: [
          {required: true, message: "布料厂商名称不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询布料列表 */
    getList() {
      this.loading = true;
      listFabric(this.queryParams).then(response => {
        this.fabricList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fabricId: null,
        fabricCode: null,
        fabricName: null,
        fabricImagePath: null,
        nowInventory: null,
        alertLessThanCount: null,
        fabricManufacturer: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fabricId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加布料信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fabricId = row.fabricId || this.ids
      getFabric(fabricId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改布料信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fabricId != null) {
            updateFabric(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFabric(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fabricIds = row.fabricId || this.ids;
      this.$modal.confirm('是否确认当前选择的数据项？').then(function () {
        return delFabric(fabricIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/fabric/export', {
        ...this.queryParams
      }, `fabric_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
